package controllers;

import com.google.gson.JsonObject;
import constants.Default;
import constants.TplPath;
import constants.session.Session;
import constants.system.GuestAuthorization;
import constants.system.MemberShip;
import duppentity.dto.sys.SysItemDto;
import play.mvc.Controller;
import play.mvc.With;
import utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

public class Application extends ApiController {

    public static void index() {
        String path = TplPath.indexPage;
        MemberShip memberShip = MemberShip.current();
        String name = memberShip.getName();
        Session session = Session.current();
        String userKey =  memberShip.getId().toString().concat("_userItemList");
        List<SysItemDto> sysItemDtoList =(List<SysItemDto>)session.get(userKey);
        String menus = getLoginUserItem(sysItemDtoList);
        renderTemplate(path,name,menus);
    }

    /**
     * 获取登录成功用户的item转换
     *       {
     *                   id: '1',
     *                   parentId: '0',
     *                   menuName: '系统设置',
     *                   icon: 'layui-icon layui-icon-set',
     *                   type: 'L',
     *                   childMenu: [
     *                       {
     *                           id: '2', parentId: '1', menuName: '日志管理', type: 'L', childMenu: [
     *                               { id: '3', parentId: '2', menuName: '登陆日志', type: 'M', url: '/log/login', },
     *                               { id: '4', parentId: '2', menuName: '操作日志', type: 'M', url: '/log/operation', },
     *                               {
     *                                   id: '5', parentId: '2', menuName: '第三方日志', type: 'L', childMenu: [
     *                                       {
     *                                           id: '6',
     *                                           parentId: '5',
     *                                           menuName: '百度首页',
     *                                           type: 'M',
     *                                           target: '_blank',
     *                                           url: 'http://www.baidu.com'
     *                                       }
     *                                   ]
     *                               }
     *                           ]
     *                       },
     *                   ]
     *               }
     * @return
     */
    private static String getLoginUserItem(List<SysItemDto> sysItemDtoList){

        List<JsonObject> zeroList = new ArrayList<>();
        List<JsonObject> oneList = new ArrayList<>();

        if (sysItemDtoList.size() > 0) {
            for (Integer i = 0; i < sysItemDtoList.size(); i++) {
                SysItemDto itemDto = sysItemDtoList.get(i);
                JsonObject tmp = new JsonObject();
                if (itemDto.level.equals(Default.itemLevel.zero)) {
                    tmp.addProperty("id", itemDto.id);
                    tmp.addProperty("parentId", itemDto.parentId);
                    tmp.addProperty("menuName", itemDto.name);
                    tmp.addProperty("icon", itemDto.style);
                    tmp.addProperty("type","L");
                    zeroList.add(tmp);
                } else if (itemDto.level.equals(Default.itemLevel.one)) {
                    tmp.addProperty("id", itemDto.id);
                    tmp.addProperty("parentId", itemDto.parentId);
                    tmp.addProperty("menuName", itemDto.name);
                    tmp.addProperty("type","M");
                    tmp.addProperty("url",itemDto.itemHref);
                    oneList.add(tmp);
                }
            }
        }

        for (Integer a = 0; a < zeroList.size(); a++) {
            //顶级
            JsonObject aJson = zeroList.get(a).getAsJsonObject();
            List<JsonObject> oneContents = new ArrayList<>();
            for (Integer b = 0; b < oneList.size(); b++) {

                JsonObject bJson = oneList.get(b).getAsJsonObject();
                if (bJson.get("parentId").getAsLong() == aJson.get("id").getAsLong()) {
                    JsonObject content1 = new JsonObject();
                    content1.addProperty("id", bJson.get("id").getAsString());
                    content1.addProperty("parentId", bJson.get("parentId").getAsString());
                    content1.addProperty("menuName", bJson.get("menuName").getAsString());
                    content1.addProperty("type", bJson.get("type").getAsString());
                    content1.addProperty("url", bJson.get("url").getAsString());
                    oneContents.add(content1);
                }

                aJson.add("childMenu", JsonUtils.toJsonTree(oneContents));
            }
        }
        String dataJson = JsonUtils.formatJson(zeroList).replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2");
        return  dataJson;
    }

    public static void home() {
        String path = TplPath.home;
        renderTemplate(path,"test");
    }

    public static void logout() {
        MemberShip.current().logout();
        apiOk("退出成功");
    }

}